<template>
  <div class="app-container home">
    <div class="bg_fff p_10 listbody">
      <div class="flexrowbetween flexwrapper" style="align-items: flex-start">
        <div class="flex">
          <div
            class="iconbox flexcenter commhover mr_10"
            @click="resetQuery"
          >
            <i class="el-icon-refresh f-20" :class="{ rotate: isrefresh }"></i>
          </div>
          <el-button plain type="primary" size="mini" @click="handleAdd">添加</el-button>
          <el-button plain type="danger" :disabled="multiple" size="mini" @click="handleDelete">删除</el-button>
        </div>
        <div>
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            label-width="0"
          >
            <el-form-item prop="keyword">
              <el-input
                class="searchinput"
                v-model="queryParams.keyword"
                placeholder="请输入素材名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- <el-form-item>
              <el-dropdown trigger="click" @command="handleCommand">
                <el-button size="mini" class="nol_border plr_9">
                  <svg-icon class-name="f-14" icon-class="export" />
                  <i class="el-icon-caret-bottom el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item
                    v-for="(item, index) in exportlist"
                    :key="index"
                    :command="item.id"
                    >{{ item.name }}</el-dropdown-item
                  >
                </el-dropdown-menu>
              </el-dropdown>
            </el-form-item> -->
            <el-form-item>
              <el-button class="f-14"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
                ></el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        ref="tablelayout"
        class="commtable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column align="center"
          label="ID"
          prop="id"
          width="120"
        ></el-table-column>
        <el-table-column align="center"
          label="素材名称"
          prop="name"
        ></el-table-column>
        <el-table-column align="center"
          label="排序"
          prop="level"
        ></el-table-column>
        <el-table-column label="显示素材"
          align="center"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#eee"
              :inactive-value="0"
              :active-value="1"
              @change="changeStatus($event,scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
            prop="created_at"
            label="操作"
            width="160"
            fixed="right"
            align="center"
        >
          <template slot-scope="scope">
            <div class="flex btnflex">
                <div class="commhover btn_green c_fff mr_10"
                    @click="handleSort(scope.row)">
                    <i class="el-icon-sort f-14"></i>排序
                </div>
                <div class="commhover btn_green c_fff"
                    @click="handleUpdate(scope.row)">
                    <i class="el-icon-edit f-14"></i> 编辑
                </div>
                <!-- <div class="commhover btn_yellow c_fff"
                    @click="handleDelete(scope.row)">
                    <i class="el-icon-delete f-14"></i> 删除
                </div> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.num"
        @pagination="getList"
      />
    </div>
    <el-dialog :title="title"
      :visible.sync="open"
      width="580px"
      class="dialogheaderbg"
      :close-on-click-modal='false'
      :before-close="handleCloseAdd">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position="left">
        <el-form-item label="素材分类名称:" prop="name" v-if="type !== 3">
          <el-input v-model="form.name" placeholder="请输入素材分类名称" />
        </el-form-item>
        <el-form-item label="排序值:" prop="level" v-if="type !== 2">
          <el-input v-model="form.level" placeholder="排序值（0-10000）" type="number" />
        </el-form-item>
        <el-form-item label="状态:" v-if="type !== 3">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="handleCloseAdd">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getClassIndex, classAdd, classEdit, classLevelEdit,classStatusEdit, classDel } from '@/api/material'
export default {
  data() {
    return {
      queryParams: {
        page: 1,
        num: 10,
        keyword: undefined,
      },
      total: 0,
      loading: false,
      tableData: [],
      isrefresh: false,
      multiple: true,
      title: "",
      // 是否显示弹出层
      type:1,
      open: false,
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "排序值不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    };
  },

  // props: {
  //   type: {
  //     type: Number,
  //     default: 1,
  //   },
  // },

  created() {
    this.getList()
  },

  methods: {
    getList() {
      this.loading = true;
      let params = {
        page: this.queryParams.page,
        num: this.queryParams.num
      }
      if(this.queryParams.keyword){
        params.keyword = this.queryParams.keyword
      }
      getClassIndex(params).then((response) => {
        this.tableData = response.data.list;
        this.total = response.data.count;
        this.loading = false;
        this.isrefresh = false;
      });
    },

    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },

    resetQuery() {
      this.isrefresh = true;
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleSelectionChange(selection) {
      this.rowids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },

    reset(){
      this.form = {
        id: undefined,
        name: undefined,
        level:undefined,
        status: 1,
      };
      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.type = 1;
      this.open = true;
      this.title = "添加";
    },

    handleCloseAdd(){
      this.open = false
    },
    
    handleCommand(command) {
      console.log(command);
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.type = 2;
      this.form.id = row.id;
      this.form.name = row.name
      this.form.status = row.status
      this.open = true;
      this.title = "修改";
    },
    // 排序
    handleSort(row) {
      this.reset();
      this.type = 3;
      this.form.id = row.id;
      this.form.level = row.level
      this.open = true;
      this.title = "修改";
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.type == 2) {
            classEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else if (this.type == 1)  {
            classAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }else if (this.type == 3){
            classLevelEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    changeStatus(val,row){
      console.log('---:', val)
      let info = val?'开启':'关闭'
      this.$modal.confirm('是否'+info+'素材展示').then(function() {
        return classStatusEdit({
          id:row.id,
          status:val
        });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(val?"开启成功":'关闭成功');
      }).catch(() => {
        console.log('jjjjjjj')
        row.status = val?0:1
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.id || this.rowids;
      this.$modal.confirm('是否确认删除访问编号为"' + infoIds + '"的数据项？').then(function() {
        return classDel({id:infoIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.iconbox {
  width: 32px;
  height: 32px;
  border: 1px solid #eee;
  border-radius: 2px;
}
</style>
